import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('cart', () => {
  const carts = ref([])

  const addCart = (cartData, cartId) => {
    let token = localStorage.getItem('token')
    let Cart = carts.value.find((item) => item.username == username)
    let pdCart = Cart.value.find((item) => item.goods_id == cartId)
    if (pdCart) {
      pdCart.number++;
    }
    else {
      carts.value.push({
        username: username,
        goods: cartData,
        number: 1,
        id:Math.random()*100000
      })
    }
  }

  const detail = (id) => {
    return carts.value.filter((item) => item.goods_id == cartId)

  }
  return { carts, addCart, detail }
}
  ,

  {
    persist: true,
  },)